package com.agencia.dominio;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.agencia.constant.Constants;





public class errors {
	private static errors INSTANCE = null;
	private errors(){}
	
	private synchronized static void createInstance(){
		if(INSTANCE == null){
			INSTANCE = new errors();
		}
	}
	
	public static errors getInstance(){
		createInstance();
		return INSTANCE;
	}
	
	public void writeError(String error) {
		Date fecha = new Date();
		SimpleDateFormat date_out = new SimpleDateFormat("yyyyMMdd");
		SimpleDateFormat dateLog = new SimpleDateFormat("yyyyMMdd-HH:mm:ss");		
		String pathFolder = Constants.getFolderLog()+"/";
		String path = Constants.getFolderLog()+"/"+ date_out.format(fecha) +"."+ Constants.getExtLog();
		File folder = new File(pathFolder);
		File file = new File(path);
		
		if(!folder.isDirectory()){
			if(folder.mkdir()){
				writeError(Constants.getFilelogcreate());
			}else{
				System.out.println(Constants.getFilelognotcreate());
			}
		}
		try {
			file.createNewFile();
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}	


		FileWriter fileLog = null;
		PrintWriter pw = null;
		
		try {
			fileLog = new FileWriter(file, true);
			pw = new PrintWriter(fileLog);
			
			pw.println(dateLog.format(fecha)+"          "+ error);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}finally{
			try {
				if(fileLog != null){
					fileLog.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
				System.out.println(e2.getMessage());
			}
		}
	}
}
